#include "svgwidget.h"

SvgWidget::SvgWidget(QWidget *parent) : QWidget(parent), svgRenderer(this)
{
    setMinimumSize(200, 200);
}

void SvgWidget::setSvgFile(const QString &filePath)
{
    currentFilePath = filePath;
    svgRenderer.load(filePath);
    update();
}

void SvgWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QRectF sourceRect = svgRenderer.viewBoxF();
    QSizeF scaledSize = sourceRect.size().scaled(size(), Qt::KeepAspectRatio);
    QRectF targetRect(QPointF(0, 0), scaledSize);
    targetRect.moveCenter(rect().center());
   /* QRectF targetRect(0.0, 0.0, width(), height());
    painter.setViewport(rect()); */
    svgRenderer.render(&painter, targetRect);
}

void SvgWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    update();
}
